
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Atienza-Home
 */
public class PickBasketFrame2 extends javax.swing.JFrame {

    /**
     * Creates new form InitMain
     */
    ArrayList<Fruit> arrfruits;
    ArrayList<JButton> fbutton1; 
    ArrayList<JButton> fbutton2;
    ArrayList<JButton> fbutton3 = new ArrayList<JButton>();
    ArrayList<JPanel> fpanel = new ArrayList<JPanel>();
    ArrayList<JToggleButton> baskets;
    Basket[] bcontain;
    ButtonGroup bgroup ;
    ArrayList<Integer> count;
    int fruitcount;
    
    
    
    public PickBasketFrame2(ArrayList<Fruit> arrfruits_, ArrayList<JButton> fbutton1_, ArrayList<JButton> fbutton2_, ArrayList<JButton> fbutton3_, ArrayList<JPanel> fpanel_, ArrayList<JToggleButton> baskets_, Basket[] bcontain_, ButtonGroup basketgroup) {
        setVisible(true);
        setResizable(false);
        arrfruits = arrfruits_;fbutton1=fbutton1_;fbutton2=fbutton2_;
        baskets=baskets_;bcontain=bcontain_;bgroup = basketgroup;
        
        for(int i=0;i<arrfruits.size();i++){
            JButton button = new JButton(String.valueOf(arrfruits.get(i).getQuantity()));
            fbutton3.add(button);
            
        }
        fruitcount = arrfruits.size();
        initComponents();
        count=new ArrayList<Integer>();
        int max=0;
        for(int i=0;i<arrfruits.size();i++){
            
            
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(3,1));
            
            fbutton1.get(i).setEnabled(false);
            panel.add(fbutton1.get(i));
                        
            fbutton2.get(i).setEnabled(false);
            panel.add(fbutton2.get(i));
            fbutton3.get(i).setEnabled(false);
            panel.add(fbutton3.get(i));
            
            fpanel.add(panel);
            if(fbutton3.get(i).getText().equals("0")){
                fpanel.get(i).setVisible(false);
                fruitcount--;
            }
            if(max<Integer.parseInt(fbutton3.get(i).getText())){
                max=Integer.parseInt(fbutton3.get(i).getText());
                for(int j=0;j<fbutton1.size();j++){
                    fbutton1.get(j).setBackground(null);
                    fbutton2.get(j).setBackground(null);
                    fbutton3.get(j).setBackground(null);
                    
                }
                
                fbutton1.get(i).setBackground(Color.green);
                fbutton2.get(i).setBackground(Color.green);
                fbutton3.get(i).setBackground(Color.green);
                
            }
            jPanel3.add(fpanel.get(i));
            jScrollPane2.setViewportView(jPanel3);
            
            
            jPanel5.add(baskets.get(i));
            bgroup.clearSelection();
            jScrollPane4.setViewportView(jPanel5);
            
            if(baskets.get(i).isVisible()==true)
                count.add(i);
            
        }
        
        Collections.shuffle(count);
        
        
        jButton5.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                
                ArrayList<String> fruits = new ArrayList<String>();
                String fruitmaxq = new String(), fruitpicked = new String();
                int basketi = bcontain.length+1;
                int bi = bcontain.length+1;
                boolean iswin=false;
                for(int i=0; i<baskets.size();i++){
                    if(baskets.get(i).isSelected()==true){
                        bi=i;
                        basketi = count.get(i);
                        break;                        
                    }                   
                }
                
                
                
                if(basketi==bcontain.length+1)
                    JOptionPane.showMessageDialog(null,"You're not yet selected any basket!");
                else{
                    
                    int maxquan=0;
                    for(int i=0;i<arrfruits.size();i++){
                        if(maxquan<arrfruits.get(i).getQuantity()){
                            maxquan=arrfruits.get(i).getQuantity();
                            fruitmaxq = arrfruits.get(i).getName();
                           
                            
                        }
                    }
                    
                    for(int i=0;i<bcontain[basketi].quantity.length;i++){
                        for(int j=0;j<bcontain[basketi].quantity[i];j++)
                            fruits.add(bcontain[basketi].fruit[i]);
                        
                    }
                   
                    Collections.shuffle(fruits);
                    fruitpicked= new String(fruits.get(0));
                    if(fruitpicked.equals(fruitmaxq))
                        iswin=true;
                    new FinalFrame2(arrfruits,fbutton1,fbutton2,fbutton3,fpanel,bcontain[basketi],baskets,bgroup,basketi,bi,fruitmaxq,fruitpicked,iswin);
                    setVisible(false);
                    
                }
                
                
                
                
            } 
        });
        
        jButton3.addActionListener(new ActionListener(){
            
            public void actionPerformed(ActionEvent e) {
                new HighestChanceFrame2(count.size(),fruitcount,arrfruits);
               
            }
        
        
        
        });
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel3.setPreferredSize(new java.awt.Dimension(635, 97));

        
        jPanel3.setLayout(new GridLayout(1,10));
        jScrollPane2.setBorder(null);
        jScrollPane2.setViewportView(jPanel3);

        jPanel5.setPreferredSize(new java.awt.Dimension(635, 97));

        
        jPanel5.setLayout(new GridLayout(1,10));
        jScrollPane4.setBorder(null);
        jScrollPane4.setViewportView(jPanel5);

        jLabel3.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        jLabel3.setText("Fruits Available");

        jLabel4.setFont(new java.awt.Font("Century Gothic", 0, 15)); // NOI18N
        jLabel4.setText("Pick A Basket");

        jButton3.setText("View Highest Chance of Survival");

        jButton4.setText("View Top 20 Chance of Survival");

        jButton5.setText("Moment of Truth");
        jButton5.setBackground(Color.red);
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
            .addComponent(jScrollPane4)
            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addContainerGap())
            .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>

    
    // Variables declaration - do not modify
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    // End of variables declaration
}
